package com.jd.product.service.impl;

import com.jd.basic.service.BaiduAiAuditService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.product.domain.Product;
import com.jd.product.domain.ProductDetail;
import com.jd.product.domain.ProductOnlineAuditLog;
import com.jd.product.mapper.ProductDetailMapper;
import com.jd.product.mapper.ProductMapper;
import com.jd.product.mapper.ProductOnlineAuditLogMapper;
import com.jd.product.service.ProductService;
import com.jd.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    @Transactional
    public void save(Product product) {
        // 保存服务信息
        super.save(product);
        //保存服务明细
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        // 删除服务明细
        productDetailMapper.delByProductId(id);
        // 删除服务信息
        super.del(id);
    }


    @Override
    @Transactional
    public void update(Product product) {
        // 修改服务信息
        super.update(product);
        // 修改服务明细
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }
    }

    @Override
    public void patchRemove(List<Long> ids) {
        productDetailMapper.patchDelByProductId(ids);
        // 批量修改
        super.patchRemove(ids);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 遍历内容
        for (Long id : ids) {
            // 上架自动审核文本
            Product product = productMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            // 审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            // 提取公共的信息
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);

            // 获取审核人信息
            Logininfo logininfo = LoginContext.getLogininfo(request);
            Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

            // 判断
            if (boo1 && boo2) { // 审核通过
                productMapper.onsale(new Date(),id);
                log.setState(1); // 表示审核成功
                log.setNote("恭喜你，审核成功了哦！");
                productOnlineAuditLogMapper.save(log);
            }else { // 审核失败
                // 记录审核日志
                log.setState(0); // 表示审核失败
                log.setNote("不好意思，审核失败了哦！");
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败了，图片、文本非法，或图片不存在！");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        productMapper.offsale(new Date(),ids);

    }
}
